package com.itheima.controller;

import com.ailyun.oss.AliyunOSSOperator;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-08 17:38
 **/
@RestController
@Slf4j
public class UploadController {
/*    private static final String UPLOAD_DIR = "D:/picture/";
    本地文件上传
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) throws IOException {
        // 获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        // 获取.+文件类型
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 获取随机对象并且拼接文件类型
        String uniqueFileName  = UUID.randomUUID().toString().replace("-", "") + extName;
        file.transferTo(new File(UPLOAD_DIR+uniqueFileName));
        return Result.success("OK");
    }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        // 获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        String url  = aliyunOSSOperator.upload(file.getBytes(), originalFilename);
        return Result.success(url);
    }
}
